package org.tang.springcloudvv.cxf.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.tang.springcloudvv.cxf.vo.User;

import java.util.List;

/**
 * <p><em>Created on 2022/4/11 5:54 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class RestTemplateUtil {

    private final static Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class);


    public static Object getContent(Object[] param)
    {
        User user = (User)param[0];
        String url = "http://127.0.0.1:10508/api/autoUserInfo/addUser";
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();

            MultiValueMap<String, String> mapParam = new LinkedMultiValueMap<>();
            mapParam.add("userId",user.getUserId());
            mapParam.add("userName",user.getUserName());
            mapParam.add("phone",user.getPhone());
            mapParam.add("age",""+user.getAge());

            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(mapParam, headers);
            //返回list
            //ParameterizedTypeReference<List<User>> responseType = new ParameterizedTypeReference<List<User>>() {};
            //ResponseEntity<List<User>> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType);

            ResponseEntity<User> response = restTemplate.postForEntity(url, requestEntity, User.class);

            return response.getBody();

        }
        catch (Exception e)
        {
            e.printStackTrace();
            User user1 = new User();
            user1.setUserId("0");
            user1.setUserName(""+e.getMessage());
            return user1;
        }
    }
}
